package com.documents.service.document;



import java.io.File;   
import java.net.ConnectException;   
import java.util.Date;   

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
  
import com.artofsolving.jodconverter.DocumentConverter; 
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;   
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;   
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; 
import com.documents.common.util.SpringBeanProxy;
/**
 * 
 * @author lvzf
 *
 */
public class JOD4DocToPDF extends java.lang.Thread {   
	private static final Log log = LogFactory.getLog(JOD4DocToPDF.class);
	  private File inputFile;// 需要转换的文件   
	    private File outputFile;// 输出的文件   
	    private Integer id;
	    public JOD4DocToPDF(File inputFile, File outputFile,Integer id) {   
	        this.inputFile = inputFile;   
	        this.outputFile = outputFile;  
	        this.id=id;
	    }   
	  
	    public void docToPdf() {   
	        Date start = new Date();   
	        // connect to an OpenOffice.org instance running on port 8100   
	        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);   
	        try {   
	            connection.connect();    
	            // convert   
	            DocumentConverter converter = new OpenOfficeDocumentConverter(   
	                    connection);   
	            converter.convert(inputFile, outputFile);  
	        } catch (ConnectException cex) { 
	            cex.printStackTrace();   
	            log.error(cex);
	        } finally {   
	            // close the connection   
	            if (connection != null) {   
	                connection.disconnect();   
	                connection = null;   
	            }   
	        }   
	        long l = (start.getTime() - new Date().getTime());   
	        long day = l / (24 * 60 * 60 * 1000);   
	        long hour = (l / (60 * 60 * 1000) - day * 24);   
	        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);   
	        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);   
	        System.out.println("生成" + outputFile.getName() + "耗费：" + min + "分" + s   
	                + "秒");   
	        //结束转换
	    DocumentFormatOrderService  documentFormatOrderService=  (DocumentFormatOrderService)SpringBeanProxy.getBean("documentFormatOrderService");
            documentFormatOrderService.endConverPdfOrder(id);
	    }   
	  
	    /**  
	     * 由于服务是线程不安全的，所以……需要启动线程  
	     */  
	    public void run() {   
	        this.docToPdf();   
	  
	    }   
	  
	    public File getInputFile() {   
	        return inputFile;   
	    }   
	  
	    public void setInputFile(File inputFile) {   
	        this.inputFile = inputFile;   
	    }   
	  
	    public File getOutputFile() {   
	        return outputFile;   
	    }   
	  
	    public void setOutputFile(File outputFile) {   
	        this.outputFile = outputFile;   
	    }   
	  
	    /**  
	     * @param args  
	     */  
	    public static void main(String[] args) {   
	    	boolean tend=false;
	        JOD4DocToPDF tools = new JOD4DocToPDF(new File("d:/aaa.doc"),   
	                new File("d:/aaa.pdf"),3);   
	        tools.start(); 
	    }   
  
} 